package com.xiaoxx.coin.handler;

import com.xiaoxx.coin.enums.HttpMethodEnum;
import com.xiaoxx.coin.enums.RequestTypeEnum;
import com.xiaoxx.coin.po.ProxyAuth;
import com.xiaoxx.common.error.ErrorException;
import com.xiaoxx.utils.RequestBuilderUtil;
import com.xiaoxx.utils.ResponseHandlerUtil;
import com.xiaoxx.utils.SignatureGeneratorUtil;
import com.xiaoxx.utils.UrlBuilderUtil;
import okhttp3.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;

public class RequestHandler {
    private final String apiKey;
    private final String secretKey;
    private static final Logger logger = LoggerFactory.getLogger(RequestHandler.class);
    private final ProxyAuth proxy;

    public RequestHandler(String apiKey, ProxyAuth proxy) {
        this.apiKey = apiKey;
        this.secretKey = null;
        this.proxy = proxy;
    }

    public RequestHandler(String apiKey, String secretKey, ProxyAuth proxy) {
        this.apiKey = apiKey;
        this.secretKey = secretKey;
        this.proxy = proxy;
    }

    public String sendPublicRequest(String baseUrl, String urlPath, LinkedHashMap<String, Object> parameters,
                                    HttpMethodEnum httpMethod, boolean showLimitUsage) {
        return sendApiRequest(baseUrl, urlPath, null, parameters, httpMethod, RequestTypeEnum.PUBLIC, showLimitUsage);
    }

    public String sendSignedRequest(String baseUrl, String urlPath, LinkedHashMap<String, Object> parameters,
                                    HttpMethodEnum httpMethod, boolean showLimitUsage) {
        if (null == secretKey || secretKey.isEmpty() || null == apiKey || apiKey.isEmpty()) {
            throw new ErrorException(400, "[RequestHandler] Secret key/API key cannot be null or empty!");
        }
        parameters.put("timestamp", UrlBuilderUtil.buildTimestamp());
        String queryString = UrlBuilderUtil.joinQueryParameters(parameters);
        String signature = SignatureGeneratorUtil.getSignature(queryString, secretKey);
        return sendApiRequest(baseUrl, urlPath, signature, parameters, httpMethod, RequestTypeEnum.SIGNED, showLimitUsage);
    }


    private String sendApiRequest(String baseUrl, String urlPath, String signature, LinkedHashMap<String, Object> parameters,
                                  HttpMethodEnum httpMethod, RequestTypeEnum requestType, boolean showLimitUsage) {
        String fullUrl = UrlBuilderUtil.buildFullUrl(baseUrl, urlPath, parameters, signature);
        logger.info("{} {}", httpMethod, fullUrl);
        Request request;
        switch (requestType) {
            case PUBLIC:
                request = RequestBuilderUtil.buildPublicRequest(fullUrl, httpMethod);
                break;
            case WITH_API_KEY:
            case SIGNED:
                request = RequestBuilderUtil.buildApiKeyRequest(fullUrl, httpMethod, apiKey);
                break;
            default:
                throw new ErrorException(400, "请求类型错误" + requestType);
        }
        return ResponseHandlerUtil.handleResponse(request, showLimitUsage, proxy);
    }
}
